
package com.compuvest.tree.upload

import javax.servlet.ServletConfig

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

class TreeProxy extends HttpServlet {

    def sql

    def void init(ServletConfig config) {
	super.init(config)

        sql = new SQLService()
    }

    def void processRequest(request, response) {

        response.setContentType("text/json;charset=UTF-8")

        def body = new StringBuilder()

        def exec = request.getParameter("exec")
        if(exec) {
            if(exec == 'getcat') {
                def pid = request.getParameter("pid"),
                active = request.getParameter("active")
                //body = sql.getAllCategories('N', pid)
                body = sql.getCategory(active, pid)
            }
            /*                if(exec == 'inactive') {
            def pid = request.getParameter("pid")
            //body = sql.getAllCategories('N', pid)
            body = sql.getCategory('N', pid)
            }
            else if(exec == 'active') {
            def pid = request.getParameter("pid")
            //body = sql.getAllCategories('Y', pid)
            body = sql.getCategory('Y', pid)
            }*/
            else if(exec == 'move') {
                def id = request.getParameter("id"),
                pid = request.getParameter("pid"),
                ids = request.getParameter("ids"),
                active = request.getParameter("active")
                
                body = sql.moveNodePanel(id, pid, ids, active)
            }
            else if(exec == 'copy') {
                def id = request.getParameter("id"), pid = request.getParameter("pid"), type = request.getParameter("type"), active = request.getParameter("active")
                body = sql.copyNodePanel(id, pid, type, active)
            }
            else if(exec == 'rename') {
                def id = request.getParameter("id"), name = request.getParameter("newname")
                body = sql.renameCategory(id, name)
            }
            else if(exec == 'insert') {
                def cid = request.getParameter("cid"), name = request.getParameter("name"), active = request.getParameter("active")
                def where = request.getParameter("where"), type = request.getParameter("type")
                body = sql.insertCategory(cid, name, active, where, (type ? type : 'C'))
            }
            else if(exec == 'remove') {
                def ids = request.getParameter("ids")
                body = sql.removeCategory(ids)
            }
        }

        def out = response.getWriter()
        try {
            out.print(body)
        }
        finally {
            out.close()
        }
    }

    def void doGet(HttpServletRequest request, HttpServletResponse response) {
        processRequest(request, response)
    }
    def void doPost(HttpServletRequest request, HttpServletResponse response) {
        processRequest(request, response)
    }
    String getServletInfo() {
        return "TreeProxy"
    }
    def void destroy() {
        sql = null
    }
}

